package store;

import beans.DatabaseTimeoutException;
import beans.ShopDB;
import beans.ShopDBException;
import java.sql.SQLException;
import store.ShopItem.Unit;
import java.util.Hashtable;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * This bean handles the items that are sold in the Store.
 * It also builds the rows of items and prices for Shopping.jsp
 * 
 * @author Dale Shpak
 */
public class ShopInventoryDB {
    private static ShopItem[] groceries;        
    
    /** Put the GroceryItems into a Hashtable so that we can get them
     *  quickly by inventory number */
    private static Hashtable<Integer,ShopItem> inventoryTable;

    static {
        ShopDB instance = null;
        try {
            instance = new ShopDB();
        } catch (ClassNotFoundException ex) {
            System.out.println("ClassNotFoundException tripped. " + ex.getMessage());
        } catch (DatabaseTimeoutException ex) {
            System.out.println("DatabaseTimeoutException tripped. " + ex.getMessage());
        } catch (ShopDBException ex) {
            System.out.println("ShopDBException tripped. " + ex.getMessage());
        } catch (SQLException ex) {
            System.out.println("SQLException tripped. " + ex.getMessage());
        }
        
        try {
            inventoryTable = instance.getListFromTable();
        } catch (DatabaseTimeoutException ex) {
            System.out.println("DatabaseTimeoutException tripped. " + ex.getMessage());
        } catch (ShopDBException ex) {
            System.out.println("ShopDBException tripped. " + ex.getMessage());
        } catch (SQLException ex) {
            System.out.println("SQLException tripped. " + ex.getMessage());
        }
        
        groceries = new ShopItem[inventoryTable.size()];
        inventoryTable.values().toArray(groceries);
    }
    
    /**
     * Gets the number of items in the list.
     * This is used when do determine when we have finished the list of items
     * @returns The number of items
     */
    public static int getNumberOfItems () {
        System.out.println ("NumberOfItems=" + groceries.length);
        return groceries.length;
    }    
    
	
    /**
     * Generates the HTML for a page of items
     * @returns A String containing all of the necessary HTML
     */
    public static String getNextItems (int currentItemNumber, int itemsPerPage) {
        StringBuffer sb = new StringBuffer(1024);
        sb.append("<table>");
        sb.append("<tr><th>Quantity</th><th>Item</th><th>Price</th><th>Unit</th></tr>\r\n");
        for (int items = 0; items < itemsPerPage && currentItemNumber < groceries.length; items++,currentItemNumber++) {
            ShopItem item = groceries[currentItemNumber];
            sb.append("<tr><td><INPUT TYPE = \"text\" NAME=\"" + item.getInventoryNumber() +"\" VALUE=\"0\" SIZE=3></td> ");
            sb.append("<td>" + item.getName() + "</td><td>" +
                    item.getPrice() + "</td><td>" + item.getUnit() + "</td></tr>\r\n");
        }
        sb.append("</table>");
        sb.append("<input type=\"hidden\" name=\"itemNumber\" value=\"" + currentItemNumber + "\" >\r\n");

        return sb.toString();
    }
    /**
     * Gets an array containing REFERENCES to a subset of the inventory items
     *
     * @param currentItemNumber The starting index into the inventory list
     * @param itemsPerPage The number of items to get
     * @return the subarray
     */
    public static ShopItem[] getArrayOfItems () {
        ShopItem[] itemArray = new ShopItem[groceries.length];
        for (int i=0; i < groceries.length; i++) {
            itemArray[i] = groceries[i].clone();
        }
        return itemArray;
    }

    /**
     * Returns a ShopItem from the inventory
     *
     * @param inventoryNumber The stock number
     * @return That ShopItem
     */
    public static ShopItem getShopItem(int inventoryNumber) {
        return inventoryTable.get(inventoryNumber);
    }
}